// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require("base");
    var router = require("../hash/pageRouter");
    var css_this = require("./newTaskDetail.css");
    var dialog = require("dialog");
    var validate = require("validate");
    var tools = require("tools");
    var databind = require("databind");
    var html_uploadTallyingFile = require("./html_uploadTallyingFile.html");
    var bookingSpaceNumber;
    var pageIndex = 1,
        pageSize = 10,
        totalPageCount = 1,
        totalRecordCount = 0; //分页初始化
    var imgSrc;
    var recieveNoticeId;
    var noticeState;
    var left;
    var financeId;
    var init = function(page){
    	recieveNoticeId = page.params.recieveNoticeId||base.getUrlParam("recieveNoticeId");
    	noticeState = page.params.noticeState||base.getUrlParam("noticeState");
    	left = base.getUrlParam("left");
    	if(left==null){
			left=1;
		}
    	showNewTaskDetail(recieveNoticeId);
		initEvent();
		router.init();
    };
	var initEvent = function(){
		//若未登录，则返回登陆页面
		if(base.getData("sid")==null||base.getData("sid")==""||base.getData("sid")==undefined){
			top.location.href="../login/login.html";
		}
		//点击详情
		$(".page-content").on("click",".taskDetail",function(){
			router.route("newTaskList/newTaskDetail.html?recieveNoticeId="+recieveNoticeId+"&noticeState="+noticeState+"&left="+left,true);
		});
		
		//点击
		$(".newTaskList").unbind("click").click(function(){
			top.location.href = "../newTaskList/newTaskList.html?noticeState="+noticeState+"&left="+left;
		});
		
		//查看理货任务
		$(".page-content").on("click",".undoneCargoRecord",function(){
			router.route("newTaskList/undoneCargoRecord.html?recieveNoticeId="+recieveNoticeId,true);
		});
		
		$(".uploadTallyingFile").unbind("click").click(function(){
			layer.open({
			  type: 1,
			  title: "上传理货任务",
			  skin: 'layui-layer-rim', //加上边框
			  area: ['820px', '500px'], //宽高
			  content: html_uploadTallyingFile,
			  btn: ['确定', '取消'],
			  yes: function(){
			  	databind.submitDom($("#uploadTallyingFile"), function(){
			  		base.alert(1, "上传成功！");
			  		router.route("newTaskList/newTaskDetail.html?recieveNoticeId="+recieveNoticeId+"&noticeState="+noticeState+"&left="+left,true);
			  	});
			  }
			});
			
			$("#uploadTallyingFile input[name=recieveNoticeId]").val(recieveNoticeId);
			
			$("#uploadTallyingFile").on("change", "#inventoryFile", function(){
				tools.uploadFile("inventoryFile", function(data){
					$("#inventoryFile").parent().find(".hidInput").val(data.data[0].fileUuid)
					var str = '<div class="showProductIntroDiv"><a target="_blank" class="mas" fileUuid="' + data.data[0].fileUuid + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + data.data[0].fileUuid + '" class="showProductIntro">'+ data.data[0].fileName +'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a class="deleteProductIntro">x</a></div>'
					$($("#uploadTallyingFile span.ff")[0]).html(str);
				});
			});
			
			$("#uploadTallyingFile").on("change", "#inventoryImage", function(){
				tools.uploadFile("inventoryImage", function(data){
					$("#inventoryImage").parent().find(".hidInput").val(data.data[0].fileUuid)
					var str = '<div class="showProductIntroDiv"><a target="_blank" class="mas" fileUuid="' + data.data[0].fileUuid + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + data.data[0].fileUuid + '" class="showProductIntro">'+ data.data[0].fileName +'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a class="deleteProductIntro">x</a></div>'
					$($("#uploadTallyingFile span.ff")[1]).html(str);
				});
			});
			
			tools.loadSelectList($("#uploadTallyingFile select[name=clerkId]"), "inquireLogisticsClerkPageList.json?pageSize=1000", "clerkId", "clerkName", "clerkMobile");
			
			$("#uploadTallyingFile").on("click", ".deleteProductIntro", function(){
				$(this).parents(".requireInput").find(".hidInput").val("");
				$(this).parents(".ff").html("");
			});
		});
		
		//接受理货任务
		$(".page-content-main").on("click",".acceptTasks",function(){
			dialog.showConfirmDialog("理货通知","确认要接受此理货通知么？",function(){
				base.fnAjax({
		            action: "confirmRecieveNotice.json",
		            oData: {
		            	recieveNoticeId: recieveNoticeId
		            }, //传递参数
		            fnSuccess: function(data) {
		                if (data.status == 200) {
							 base.alert(1,"确认理货任务成功");
							 router.route("newTaskList/newTaskDetail.html?recieveNoticeId="+recieveNoticeId,true);
		                } else {
		                     base.alert(2,"信息获取失败");
		                }
		            }
		        });
			});
		});
		
		//取消理货任务
		$(".page-content-main").on("click",".opposeTasks",function(){
			dialog.showConfirmDialog("理货通知","确认要拒绝此理货通知么？",function(){
				base.fnAjax({
		            action: "cancelRecieveNotice.json",
		            oData: {
		            	recieveNoticeId: recieveNoticeId
		            }, //传递参数
		            fnSuccess: function(data) {
		                if (data.status == 200) {
							 base.alert(1,"拒绝理货任务成功");
							 router.route("newTaskList/newTaskDetail.html?recieveNoticeId="+recieveNoticeId,true);
		                } else {
		                     base.alert(2,"信息获取失败");
		                }
		            }
		        });
			});
		});
		
		//完成理货任务
		$(".page-content-main").on("click",".finishedTasks",function(){
			dialog.showConfirmDialog("完成理货任务","确定要完成此理货任务吗",function(){
				base.fnAjax({
            action: "finishedRecieveNotice.json",
            oData: {
            	recieveNoticeId: recieveNoticeId
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
					 base.alert(1,"理货任务完成");
					 location.href = "../newTaskList/newTaskList.html?noticeState=2&left=2";
                } else {
                     base.alert(2,"信息获取失败");
                }
            }
        });
			});
		});
		//生成理货单
		$(".page-content-main").on("click",".tallyListGenerate",function(){
			router.route("newTaskList/tallyListGenerate.html?recieveNoticeId="+recieveNoticeId+"&left="+left+"&noticeState="+noticeState,true);
			});
    };
    
     var showNewTaskDetail = function(recieveNoticeId){
    	base.fnAjax({
            action: "inquireRecieveNoticeDetail.json",
            oData: {
            	recieveNoticeId: recieveNoticeId
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	bookingSpaceNumber = data.datas[0].bookingSpaceNumber;
                	financeId = data.datas[0].financeId;
                	if(financeId!=0) {
                		inquireFinanceApplicationDetail(financeId);
                	} else {
                		inquireFinanceContainerBillList(data.datas[0].recieveNoticeId);
                	}
                	var html ;
                	var newTaskDetailTpl = $("#newTaskDetailTpl").html();
					if(newTaskDetailTpl!=undefined){
                		html = juicer(newTaskDetailTpl, data);
                	}
					$(".page-content-main-content").html(html);
					
					databind.fillForm(".dialogContentNewTask",{recieveNoticeId:recieveNoticeId});
					var noticeState = data.datas[0].noticeState;
					if(noticeState==1){
						$(".acceptTasks").css("display", "inline-block");
						$(".opposeTasks").css("display", "inline-block");
						$(".tallyListGenerate").css("display", "none");
						$(".bar").css("display", "none");
						$(".undoneCargoRecord").css("display", "none");
						 
					}else if(noticeState==2){
						$(".acceptTasks").css("display", "none");
						$(".opposeTasks").css("display", "none");
						$(".tallyListGenerate").css("display", "inline-block");
						$(".bar").css("display", "inline-block");
						$(".undoneCargoRecord").css("display", "inline-block");
						$(".finishedTasks").css("display", "inline-block");
					}
					
					if (data.datas[0].financeId ==0) {
						$(".finishedTasks").remove();
					}
					
					if (data.datas[0].productType == 4) {
						$("#newTaskDetail .opposeTasks").remove();
						$("#newTaskDetail .acceptTasks").html("确认");
					}
                } else {
                     base.alert(2,"信息获取失败");
                }
            }
        });
    }
     
    var inquireFinanceApplicationDetail = function(financeId){
    	base.fnAjax({
            action: "inquireFinanceApplicationDetail.json",
            oData: {
            	financeId: financeId
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	juicer.register("inquireFile", tools.inquireFile);
                	juicer.register("formatCurrency", tools.formatCurrency);
                	
					var html ;
                	var newTaskDetailTpl = $("#financeDetailTpl").html();
					if(newTaskDetailTpl!=undefined){
                		html = juicer(newTaskDetailTpl, data);
                	}
					$(".financeDetail").html(html);
					
					//运费保理显示运费信息
					if (data.datas[0].productType == 4) {
						var tpl = $("#freightFactoringTpl").html();
						var html = juicer(tpl, data);
						$("#newTaskDetail .pledging_goods").after(html);
						$("#newTaskDetail .pledging_goods").remove();
					}
					
					$.each($("table"), function(){
						if ($(this).find("tr").length < 2) {
							$(this).remove();
						}
					});
					
					var receivableAmountSum = 0;
					$.each($("#newTaskDetail .receivableAmount"), function() {
						receivableAmountSum = receivableAmountSum*1 + $(this).html()*1;
					});
					$("#newTaskDetail .receivableAmountSum").html(tools.formatCurrency(receivableAmountSum));
					
					var factoringAmountSum = 0;
					$.each($("#newTaskDetail .factoringAmount"), function() {
						factoringAmountSum = factoringAmountSum*1 + $(this).html()*1;
					});
					$("#newTaskDetail .factoringAmountSum").html(tools.formatCurrency(factoringAmountSum));
					
					var invoiceAmountSum = 0;
					$.each($("#newTaskDetail .invoiceAmount"), function() {
						invoiceAmountSum = invoiceAmountSum*1 + $(this).html()*1;
					});
					$("#newTaskDetail .invoiceAmountSum").html(tools.formatCurrency(invoiceAmountSum));
					
					$("#newTaskDetail .freightQuantity").html($("#newTaskDetail .freight").length);
					$("#newTaskDetail .waybillQuantity").html($("#newTaskDetail .waybillNumber").length)
					
                } else {
                     base.alert(2,"信息获取失败");
                }
            }
        });
    }
    
    var inquireFinanceContainerBillList = function(recieveNoticeId){
    	base.fnAjax({
            action: "inquireFinanceContainerBillList.json",
            oData: {
            	recieveNoticeId: recieveNoticeId
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
					var html ;
                	var newTaskDetailTpl = $("#orderDetailTpl").html();
					if(newTaskDetailTpl!=undefined){
                		html = juicer(newTaskDetailTpl, data);
                	}
					$(".financeDetail").html(html);
					$(".bookingSpaceNumber").html("订舱单号："+bookingSpaceNumber);
                } else {
                     base.alert(2,"信息获取失败");
                }
            }
        });
    }
     
    return {
        init:init
    };
});